﻿using WRJClient.UI.Map;
using WRJClient.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using WRJClient.Utils;
using WRJClient.Common;

namespace WRJClient.ViewModel
{
    public class ControlCenterViewModel : BaseViewModel<WorkStationModel>, IMapObject
    {
        private BaseGraphic _gBody = new BaseGraphic();
        private BaseGraphic _gLabel = new BaseGraphic();

        public bool Visable { get { return _gBody.IsVisible || _gLabel.IsVisible; }
            set
            {
                _gBody.IsVisible = value;
                _gLabel.IsVisible = value;
            }
        }
        private bool _isOnline = false;

        public bool IsOnline
        {
            get
            {
                return _isOnline;
            }
            set
            {
                if (_isOnline != value)
                {
                    _isOnline = value;
                    if (_isOnline)
                    {
                        _onlineTimer.Start();
                    }
                    else
                    {
                        _onlineTimer.Stop();
                    }
                    OnPropertyChanged();
                    InitGraphics();
                }
            }
        }


        public bool IsVisable
        {
            get { return _gBody.IsVisible; }
            set { if (_gBody.IsVisible != value) { _gBody.IsVisible = value; OnPropertyChanged(); }; }
        }
        public bool IsLabelVisable
        {
            get { return _gLabel.IsVisible; }
            set { if (_gLabel.IsVisible != value) { _gLabel.IsVisible = value; OnPropertyChanged(); }; }
        }
        public bool IsEnabled { get { return ArcgisUtils.IsExtentValid(_gBody.Geometry.Extent); } }

        private TimeOut _onlineTimeout = null;
        private DispatcherTimer _onlineTimer = null;

        public ControlCenterViewModel(WorkStationModel model = null) : base(model)
        {
            InitGraphics();
            _onlineTimeout = new TimeOut(5 * 1000);
            _onlineTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
            _onlineTimer.Tick += _onlineTimer_Tick;
        }

        private void _onlineTimer_Tick(object sender, EventArgs e)
        {
            if (_onlineTimeout.IsTimeout())
            {
                IsOnline = false;
            }
        }
        public void HeartBeat(double longitude, double latitude)
        {
            StaticData.Longitude = longitude;
            StaticData.Latitude = latitude;

            IsOnline = true;
            _onlineTimeout.Restart();
        }

        public void InitGraphics()
        {
            string status = "online";
            if (IsOnline)
            {
                status = "online";
            }
            else
            {
                status = "offline";
            }

            GraphicFactory.CreateImageGraphic(_gBody, StaticData.DbId, GraphicPart.Base, SymbolHelper.Instance.GetPictureMarkerSymbolInfo(GetType(), "commandcenter", status), StaticData.Longitude, StaticData.Latitude);
            GraphicFactory.CreateTextGraphic(_gLabel, StaticData.DbId, GraphicPart.Label, SymbolHelper.Instance.GetTextSymbolInfo(GetType()), StaticData.Name, StaticData.Longitude, StaticData.Latitude);

        }

        public void AddGraphics()
        {
            MapManager.Instance.AddGraphic("Layer1", _gBody);
            MapManager.Instance.AddGraphic("Layer4", _gLabel);
        }

        public void RemoveGraphics()
        {
            MapManager.Instance.RemoveGraphic("Layer1", _gBody);
            MapManager.Instance.RemoveGraphic("Layer4", _gLabel);
        }
    }
}
